package net.example.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import net.example.activity.adapter.GroupNotifyAdpter;
import net.example.activity.adapter.interfaces.IHandleOnClickCallback;
import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.AddGroupRequest;
import net.example.model.AddGroupRequestPage;
import net.example.model.GroupNotifyModel;
import net.example.model.GGGroup;
import net.example.model.GGUser;
import net.example.model.event.GetAddGroupPageEvent;
import net.example.model.event.HandleRequestAddGroupReceivedEvent;
import net.example.ggtalk.R;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class GroupNotifyActivity extends Activity implements IHandleOnClickCallback<GroupNotifyModel> {
    private GroupNotifyAdpter adpter;
    private ListView listView;
    private List<GroupNotifyModel> modelList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_notify);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        ((View)findViewById(R.id.ct_photo)).setVisibility(View.GONE);
        TextView title = (TextView) findViewById(R.id.headerText);
        title.setText("群验证");
        Manager.getInstance().getAddGroupPage();
        listView = (ListView) findViewById(R.id.lv_friendNotify);
        adpter = new GroupNotifyAdpter(this, modelList, R.layout.friend_request_item);
        adpter.setHandleOnClickCallback(this);
        listView.setAdapter(adpter);
    }

    public void onEventMainThread(GetAddGroupPageEvent event)
    {
        modelList.clear();
        AddGroupRequestPage page = event.getAddGroupRequestPage();
        if (page == null || page.AddGroupRequestList == null) {
            adpter.notifyDataSetChanged();
            return;
        }
        String currentUserID= ClientGlobalCache.getInstance().getCurrentUser().getUserID();
        for (AddGroupRequest item : page.AddGroupRequestList) {
            GGUser requseter;
            GGGroup group = ClientGlobalCache.getInstance().getGroup(item.GroupID);
            if (item.RequesterID.equals(currentUserID)) {
                requseter = ClientGlobalCache.getInstance().getCurrentUser();
                modelList.add(new GroupNotifyModel(item, group, requseter, true));
            } else {
                requseter = ClientGlobalCache.getInstance().getUser(item.RequesterID);
                modelList.add(new GroupNotifyModel(item, group, requseter, false));
            }
        }
        adpter.notifyDataSetChanged();
    }

    public void onEventMainThread(HandleRequestAddGroupReceivedEvent event)
    {
        Manager.getInstance().getAddGroupPage();
    }

    @Override
    public void onAgreeClick(GroupNotifyModel groupNotifyModel) {
        Manager.getInstance().handleAddGroupRequest(groupNotifyModel.AddGroupRequest.RequesterID,groupNotifyModel.AddGroupRequest.GroupID,true);
        ClientGlobalCache.getInstance().removeRequestAddGroup(groupNotifyModel.AddGroupRequest.RequesterID,groupNotifyModel.AddGroupRequest.GroupID);
        groupNotifyModel.AddGroupRequest.State=1;
        adpter.notifyDataSetChanged();
    }

    @Override
    public void onRefuseClick(GroupNotifyModel groupNotifyModel) {
        Manager.getInstance().handleAddGroupRequest(groupNotifyModel.AddGroupRequest.RequesterID,groupNotifyModel.AddGroupRequest.GroupID,false);
        ClientGlobalCache.getInstance().removeRequestAddGroup(groupNotifyModel.AddGroupRequest.RequesterID,groupNotifyModel.AddGroupRequest.GroupID);
        groupNotifyModel.AddGroupRequest.State=2;
        adpter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void finish(View view)
    {
        finish();
    }
}
